package com.lianyu507.controller;

import com.lianyu507.bean.PagingProperties;
import com.lianyu507.enums.CommonStatusEnum;
import com.lianyu507.pojo.CrArticle;
import com.lianyu507.pojo.CrArticleExample;
import com.lianyu507.pojo.WechatUser;
import com.lianyu507.pojo.WechatUserExample;
import com.lianyu507.service.CrArticleService;
import com.lianyu507.service.WechatUserService;
import com.lianyu507.util.JsonUtils;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 */
@Controller
@RequestMapping("/wechat/index")
public class IndexController {
  private final Logger logger = LoggerFactory.getLogger(this.getClass());

  @Autowired
  private CrArticleService crArticleService;

  @Autowired
  private WechatUserService wechatUserService;

  @Autowired
  private WxMpService wxService;


  @RequestMapping
  public ModelAndView index(ModelAndView modelAndView , PagingProperties pagingProperties,@RequestParam(required = false) String code){


    CrArticleExample crArticleExample=new CrArticleExample();
    crArticleExample.or().andStatusEqualTo(CommonStatusEnum.NORMAL.getValue());
    if(pagingProperties.getNeedPaging()){
      pagingProperties.setTotal(crArticleService.countByExample(crArticleExample));
      crArticleExample.setOrderByClause( " id desc "+pagingProperties.build());

    }else{
      crArticleExample.setOrderByClause( " id desc ");
    }
    List<CrArticle>  crArticleList=this.crArticleService.selectByExample(crArticleExample);

    modelAndView.addObject("crArticleList",crArticleList);
    modelAndView.addObject("paging",pagingProperties);

    WechatUser wechatUser=new WechatUser();

    if(code!=null){
      logger.info("code:   "+code);
      String openId=null;
      try {
        WxMpOAuth2AccessToken wechatOAuth2AccessToken = wxService.oauth2getAccessToken(code);
        if(wechatOAuth2AccessToken!=null){
          openId=wechatOAuth2AccessToken.getOpenId();
          logger.info("openId:   "+openId);

        }
      } catch (WxErrorException e) {
        e.printStackTrace();
      }

      if(openId!=null){
        WechatUserExample wechatUserExample=new WechatUserExample();
        wechatUserExample.or().andWechatJsonLike("%"+openId+"%");

        // 只有判断userId 不为空的时候才需要把WechatUserExample.Criteria 内部类拿出来
//      if(userId != null){
//        condition.andUserIdEqualTo(userId);
//      }
        // example 适合简单的条件查询，比如and，like，大于等于，in，is null，is not null
        //
        // select * from wechat_user where (wechat_json like '%sss%' and user_id is null)

        List<WechatUser> wechatUserList=wechatUserService.selectByExample(wechatUserExample);
        logger.info("wechatUserList size:   "+wechatUserList.size());

        if(wechatUserList.size()>0){
          wechatUser=wechatUserList.get(0);
        }
      }


    }
    logger.info("wechatUser:   "+ JsonUtils.objectToJson(wechatUser));
    modelAndView.addObject("wechatUser",wechatUser);

    modelAndView.setViewName("index");
    return modelAndView;
  }


}
